// IM聊天
import React, {useEffect, useState} from 'react';
import {Row, Col} from 'antd';
import WeContacts from "./WeContacts";
import WeMain from "./WeMain";

// const NIM = window.NIM();
const WeChat = () => {
    // window.NIM
    // const data = {};
    // const nim = new NIM({})
    // 联系人右键
    const [contactsRightClick, setContactsRightClick] = useState<any>({
        id: null,
        isGroup: 0,
    });
    // 聊天室右键
    const [roomRightClick, setRoomsRightClick] = useState<any>({}); // 聊天室右键
    // 右键菜单
    const nodeWalk = (nodeArray: any) => {
        for (let item of nodeArray) {
            // 联系人右键菜单
            if (typeof (item.className) === 'string' && item.className.includes('WeChatContactsItemBox')) {
                const id = parseInt(item.dataset.id);
                if (id === contactsRightClick.id){
                    setContactsRightClick({
                        id: null,
                        isGroup: 0,
                    });
                } else {
                    const isGroup = parseInt(item.dataset.group);
                    setContactsRightClick({
                        id,
                        isGroup
                    });
                    setRoomsRightClick(null);
                }
                return;
            }
        }

    };
    const handleContextMenu = (event: any) => {
        event.preventDefault();
        nodeWalk(event.path)
    };
    useEffect(() => {
        document.addEventListener('contextmenu', handleContextMenu);
        return () => {
            document.removeEventListener('contextmenu', handleContextMenu);
        }
    });
    return (
        <>
            <Row>
                <Col span={5}>
                    <WeContacts contactsRightClick={contactsRightClick}/>
                </Col>
                <Col span={19}>
                    <WeMain/>
                </Col>
            </Row>
        </>
    )
};
export default React.memo(WeChat);